/*
 * FileChooser.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import java.io.File;

/**
 * File Chooser Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public final class FileChooser extends Dialog {

// Private Constructor. --------------------------------------------------------

 /**
  * Class has no instances.
  */
  private FileChooser() {
    // empty constructor body
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns directory selected by the user.
   *
   * @param parent the parent component
   *
   * @return directory selected by the user
   */
  public static String getExistingDirectoryName(Component parent) {
    return getExistingDirectoryName(parent, null, null);
  }

  /**
   * Returns directory selected by the user.
   *
   * @param parent the parent component
   * @param caption the dialog caption
   * @param directory the directory
   *
   * @return directory selected by the user
   */
  public static String getExistingDirectoryName(Component parent,
                                                String caption,
                                                String directory) {
    if (parent != null) {
      return _getExistingDirectoryName(parent._delegate, caption, directory);
    }
    return _getExistingDirectoryName(0, caption, directory);
  }

  /**
   * Returns directory selected by the user.
   *
   * @param parent the parent component
   *
   * @return directory selected by the user
   */
  public static File getExistingDirectory(Component parent) {
    return getExistingDirectory(parent, null, null);
  }

  /**
   * Returns directory selected by the user.
   *
   * @param parent the parent component
   * @param caption the dialog caption
   * @param directory the directory
   *
   * @return directory selected by the user
   */
  public static File getExistingDirectory(Component parent, String caption,
                                          String directory) {
    String fileName = getExistingDirectoryName(parent, caption, directory);
    if (fileName != null) {
      return new File(fileName);
    }
    return null;
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   *
   * @return file selected by the user
   */
  public static String getOpenFileName(Component parent) {
    return getOpenFileName(parent, null, null, null, null);
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   * @param caption the dialog caption
   * @param directory the directory
   * @param filter the file directory
   * @param selectedFilter the selected filter
   *
   * @return file selected by the user
   */
  public static String getOpenFileName(Component parent, String caption,
                                       String directory, String filter,
                                       String selectedFilter) {
    if (parent != null) {
      return _getOpenFileName(parent._delegate, caption, directory, filter,
                              selectedFilter);
    }
    return _getOpenFileName(0, caption, directory, filter, selectedFilter);
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   *
   * @return file selected by the user
   */
  public static File getOpenFile(Component parent) {
    return getOpenFile(parent, null, null, null, null);
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   * @param caption the dialog caption
   * @param directory the directory
   * @param filter the file directory
   * @param selectedFilter the selected filter
   *
   * @return file selected by the user
   */
  public static File getOpenFile(Component parent, String caption,
                                 String directory, String filter,
                                 String selectedFilter) {
    String fileName = getOpenFileName(parent, caption, directory, filter,
                                      selectedFilter);
    if (fileName != null) {
      return new File(fileName);
    }
    return null;
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   *
   * @return file selected by the user
   */
  public static String getSaveFileName(Component parent) {
    return getSaveFileName(parent, null, null, null, null);
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   * @param caption the dialog caption
   * @param directory the directory
   * @param filter the file directory
   * @param selectedFilter the selected filter
   *
   * @return file selected by the user
   */
  public static String getSaveFileName(Component parent, String caption,
                                       String directory, String filter,
                                       String selectedFilter) {
    if (parent != null) {
      return _getSaveFileName(parent._delegate, caption, directory, filter,
                             selectedFilter);
    }
    return _getSaveFileName(0, caption, directory, filter, selectedFilter);
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   *
   * @return file selected by the user
   */
  public static File getSaveFile(Component parent) {
    return getSaveFile(parent, null, null, null, null);
  }

  /**
   * Returns file selected by the user.
   *
   * @param parent the parent component
   * @param caption the dialog caption
   * @param directory the directory
   * @param filter the file directory
   * @param selectedFilter the selected filter
   *
   * @return file selected by the user
   */
  public static File getSaveFile(Component parent, String caption,
                                 String directory, String filter,
                                 String selectedFilter) {
    String fileName = getSaveFileName(parent, caption, directory, filter,
                                      selectedFilter);
    if (fileName != null) {
      return new File(fileName);
    }
    return null;
  }

// Package-Private Static Native Methods. --------------------------------------

  static native String _getExistingDirectoryName(long parentDelegate,
                                                 String caption,
                                                 String directory);

  static native String _getOpenFileName(long parentDelegate, String caption,
                                        String directory, String filter,
                                        String selectedFilter);

  static native String _getSaveFileName(long parentDelegate, String caption,
                                        String directory, String filter,
                                        String selectedFilter);

}
